# coding=utf-8

"""
@File: commons/times_tool.py
@Time: 2024/09/08 16:20
@Author: TangZuChao
@Describe:  时间工具模块
"""
import time
from datetime import datetime, timedelta

import allure


@allure.step("获取当前的日期和时间")
def get_current_time():
    current_time = datetime.now()
    return current_time

@allure.step("获取当前的日期和时间的时间戳")
def get_timestamp():
    timestamp = datetime.timestamp(get_current_time())
    return timestamp

@allure.step("获取当前的日期和时间的10位时间戳，精确到秒")
def get_second_timestamp():
    ts10 = str(get_timestamp()).split(".")[0]
    return ts10

@allure.step("获取当前的日期和时间的13位时间戳，精确到毫秒")
def get_millisecond_timestamp():
    ts13 = round(time.time() * 1000)
    return ts13

@allure.step("获取指定日期格式的时间戳")
def get_specified_date(date):
    # 将字符串转换为日期对象
    date_obj = datetime.strptime(date, "%Y-%m-%d")
    # 转换为时间戳
    timestamp = date_obj.timestamp()
    return timestamp

@allure.step("时间戳转日期")
def get_timestamp_to_date_conversion(timestamp):
    """
    时间戳转日期格式
    :param timestamp:
    :return:
    注：Windows下毫秒级的时间戳，需要除以1000才可以转成秒级
    """
    # 判断timestamp是否为整型字符
    if isinstance(timestamp, int):
        # 判断timestamp长度是否等于10，时间戳为秒级
        if len(str(timestamp)) == 10:
            return datetime.fromtimestamp(timestamp)
        return datetime.fromtimestamp(timestamp / 1000)
    else:
        # 判断timestamp字符串长度是否等于10，时间戳为秒级
        if len(timestamp) == 10:
            return datetime.fromtimestamp(int(timestamp))
        # Windows下毫秒级时间戳，除以1000转成秒级
        return datetime.fromtimestamp(int(timestamp) / 1000)

@allure.step("日期加减法")
def date_addition_and_subtraction(date_name, date_number):
    # 当前日期
    current_time = datetime.now().date()
    if date_name == "days":
        # 明天的日期
        tomorrow = current_time + timedelta(days=date_number)
        return tomorrow
    elif date_name == "weeks":
        # 一周后的日期
        last_week = current_time + timedelta(weeks=date_number)
        return last_week
    else:
        return None

